package de.hm.edu.simulation;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;

/**
 * A Thread that simulates incoming Orders
 * 
 * @author Peter Goldenbogen (Munich University of Applied Science)
 *
 */
public class Simulation1 extends Thread {

	Random generator = new Random();

	final static int HAMMER = 0;
	final static int SAW = 1;
	final static int SCREWDRIVER = 2;
	
	final static long TIMEOUT = 60000;
	final static int MAX_AMOUNT = 1200;
	
	@Override
	public void run(){
		
		try {
			sleep(10000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		System.out.println("Starting Simulation Thread");
		
		while(true){
			
			int product = generator.nextInt(3);
			int amount = generator.nextInt(MAX_AMOUNT);
			
			switch(product){
				case HAMMER:
					sendOrder("hammer", amount);
					System.out.println("Send Order: Product=Hammer Amount=" + amount);
					break;
				case SAW:
					sendOrder("saw", amount);
					System.out.println("Send Order: Product=Saw Amount=" + amount);
					break;
				case SCREWDRIVER:
					sendOrder("screwdriver", amount);
					System.out.println("Send Order: Product=Screwdriver Amount=" + amount);
					break;
			}
			
			try {
				sleep(TIMEOUT);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Sends a http request to ChartService
	 * @param product
	 * @param amount
	 */
	public void sendOrder(String product, int amount){
		  
		String url = "http://chartservice.no-ip.org/ChartService/rest/kpi/add_order";
		HttpParams params = new BasicHttpParams();
		
		HttpClient httpclient = new DefaultHttpClient(params);
	    HttpPost httppost = new HttpPost(url);
	    
	    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	    nameValuePairs.add(new BasicNameValuePair("product", product));
	    nameValuePairs.add(new BasicNameValuePair("amount","" + amount));
	    
	    try {
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			HttpResponse response = httpclient.execute(httppost);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
